package com.lagou.edu.service;

import com.lagou.edu.anno.Autowired;
import com.lagou.edu.anno.Transactional;
import com.lagou.edu.dao.UserDao;
import com.lagou.edu.pojo.Account;

/**
 * @author HZF
 * @date 2020/5/31 9:03
 * @description
 */
public class UserService {

    @Autowired
    private UserDao userDao;

    public void save(){
        userDao.save();
    }

    public Account queryOne(String carNo) throws Exception {
        Account account = userDao.queryAccountByCardNo(carNo);
        return account;
    }

    public int updateAccount(Account account) throws Exception {
        int i = userDao.updateAccountByCardNo(account);
        return i;
    }

    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = userDao.queryAccountByCardNo(fromCardNo);
        Account to = userDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);

        userDao.updateAccountByCardNo(to);
        //int c = 1/0;
        userDao.updateAccountByCardNo(from);
    }
}
